/*
 * Copyright 2008 M-Way Solutions
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mwaysolutions.ocm.impl;

import java.util.HashMap;
import java.util.Map;

import com.mwaysolutions.ocm.OcmTypeDescriptor;
import com.mwaysolutions.ocm.OcmTypeRegister;
import com.mwaysolutions.ocm.TypeNotRegisteredOcmException;

/**
 *
 * @author <a href="mailto:m.zdila@mwaysolutions.com">Martin Ždila</a>
 */
public class OcmTypeRegisterImpl implements OcmTypeRegister {

	private final Map<String, OcmTypeDescriptor> nodeTypeNameToTypeDescriptorMap = new HashMap<String, OcmTypeDescriptor>();
	private final Map<Class<?>, OcmTypeDescriptor> classToTypeDescriptorMap = new HashMap<Class<?>, OcmTypeDescriptor>();


	/* (non-Javadoc)
	 * @see com.mwaysolutions.ocm.OcmTypeRegister#register(com.mwaysolutions.ocm.OcmTypeDescriptor)
	 */
	public void register(final OcmTypeDescriptor ocmTypeDescriptor) {
		nodeTypeNameToTypeDescriptorMap.put(ocmTypeDescriptor.getNodeTypeName(), ocmTypeDescriptor);
		classToTypeDescriptorMap.put(ocmTypeDescriptor.getClazz(), ocmTypeDescriptor);
	}


	/* (non-Javadoc)
	 * @see com.mwaysolutions.ocm.OcmTypeRegister#getTypeDescriptorByNodeTypeName(java.lang.String, boolean)
	 */
	public OcmTypeDescriptor getTypeDescriptorByNodeTypeName(final String nodeTypeName, final boolean check) {
		final OcmTypeDescriptor ocmTypeDescriptor = nodeTypeNameToTypeDescriptorMap.get(nodeTypeName);
		if (check && ocmTypeDescriptor == null) {
			throw new TypeNotRegisteredOcmException(nodeTypeName);
		}
		return ocmTypeDescriptor;
	}

	/* (non-Javadoc)
	 * @see com.mwaysolutions.ocm.OcmTypeRegister#getTypeDescriptorByClass(java.lang.Class, boolean)
	 */
	public OcmTypeDescriptor getTypeDescriptorByClass(final Class<?> clazz, final boolean check) {
		final OcmTypeDescriptor ocmTypeDescriptor = classToTypeDescriptorMap.get(clazz);
		if (check && ocmTypeDescriptor == null) {
			throw new TypeNotRegisteredOcmException(clazz.getName());
		}
		return ocmTypeDescriptor;
	}

}
